/*
 * @(#)PlayerManager.java
 * 
 * Copyright(C) 2004 Yujin Kim
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.netexplode.jtunes.model.player;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.netexplode.jtunes.core.Configuration;
import com.netexplode.jtunes.core.ConfigurationAware;
import com.netexplode.jtunes.model.media.CollectionManager;
import com.netexplode.jtunes.model.media.MediaFile;

/**
 * <code>PlayerManager</code> represents .....
 * 
 * @author ykim
 * @version $Revision: 1.3 $
 * @since 0.1
 */
public class PlayerManager implements ConfigurationAware, ApplicationContextAware {

	private static final Log log = LogFactory.getLog(PlayerManager.class);

	private boolean playing;
	
	private ApplicationContext applicationContext;

	private Configuration configuration;

	private CollectionManager collectionManager;
	
	public void setApplicationContext(ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	public void setCollectionManager(CollectionManager collectionManager) {
		this.collectionManager = collectionManager;
	}

	/**
	 * Creates an instance of Jukebox with multiple root location with a given
	 * name
	 * 
	 * @param locations
	 */
	public void init() throws Exception {
		final String[] names = configuration.getLibraryNames();
		final String[] locations = configuration.getLibraryRoots();

		// @todo add validation
		for (int i = 0; i < names.length; i++) {
			log.info(names[i] + " : adding collection from " + locations[i]);
			collectionManager.getLocalCollections().add(collectionManager.newLocalCollection(names[i], locations[i]));
		}
	}

	/**
	 * @return Returns the mediaPlayer.
	 */
	public MediaPlayer getMediaPlayer() {

		MediaPlayer mediaPlayer = null;
		final MediaFile mediaFile = collectionManager.getCurrentMediaFile();

		if (mediaFile.getFormat().equalsIgnoreCase("mp3")) {
			
			mediaPlayer = (MediaPlayer) applicationContext.getBean(Mp3Player.class.getName());
		}
		else {
			mediaPlayer = (MediaPlayer) applicationContext.getBean(Mp4Player.class.getName());
			// @todo
		}

		log.debug("mediaPlayer = " + mediaPlayer);

		return mediaPlayer;
	}

	/**
	 * @return Returns the playing.
	 */
	public boolean isPlaying() {
		return this.playing;
	}

	/**
	 * @param playing The playing to set.
	 */
	public void setPlaying(boolean playing) {
		this.playing = playing;
	}
}